/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import core.UpFile;

/**
 * This class is the backbone for reading the filenames of the uploaded
 * files from the filesystem. It creates a list of UpFiles that contains
 * the filename and a link to the file, this is later used for displaying
 * what files are uploaded. It furthermore adds characters in front of the
 * filename in order to create a visual hiarchy of the files and folders.
 *
 * @author Fredrik
*/
public class FileStructureBean {

    private List<UpFile> fileList;
    private String temp = "../upload/";
    private String folderPath = "C:\\Users\\Fredrik\\Documents\\DAT075 - " +
                "Distributed Systems\\svn\\build\\web\\upload";
    int tabCounter = 0;

    @PostConstruct
    void init() {
        fileList = new ArrayList<UpFile>();
    }

    /**
     *
     * The function that does all the work, it goes through all content of the
     * specified folder and catalogues it. If the folder does not exist it
     * creates it. If also scans subfolders and orders the files so that the
     * folder layout become clear in the output.
     *
     * @param folder Folder which is to be scanned and catalogued
     */
    public void listFilesAndFolders(String folder) {

        File file = new File(folder);

        if (!file.exists() || !file.isDirectory()) {
            System.out.println("Parameter is not a directory");
            file.mkdir();
        }

        File[] fileArray = file.listFiles();
        
        for (int i = 0; i < fileArray.length; i++) {
            if (fileArray[i].isDirectory()) {
                fileList.add(new UpFile(getTabs() + "-- " + fileArray[i].getName(), ""));
                tabCounter++;
                listFilesAndFolders(fileArray[i].getAbsolutePath());
            } else {
                fileList.add(new UpFile(getTabs() + fileArray[i].getName(), temp + fileArray[i].getName()));
            }
        }
        tabCounter--;
    }

    /**
     * This creates a string of chars depening of wheter the file is in a sub
     * dir or not. Its purpose is only to help the user understand what files
     * is contained in what folders.
     *
     * @return The finished modified string of graphics needed
     */
    private String getTabs() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < tabCounter; i++) {
            buffer.append("´´ |---- ");
        }
        return buffer.toString();
    }

    public void setFileList(List<UpFile> fileList) {
        this.fileList = fileList;
    }

    /**
     *
     * This is where we call our function, mostly because its the easiest way.
     * The list is cleared before we fill it so no leftovers from preious
     * viewings remain.
     *
     * @return The finished filelist
     */
    public List<UpFile> getFileList() {
        fileList.clear();
        listFilesAndFolders(folderPath);
        return fileList;
    }
}
